home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2941 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  59 lines

  1. Path: oxy.rust.net!usenet
  2. From: ebennett@rust.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Ctors & member methods ?
  5. Date: Sat, 20 Jan 1996 22:52:44 GMT
  6. Organization: Rust Net - High Speed Internet in Detroit  810-642-2276
  7. Message-ID: <4drh56$1i2@oxy.rust.net>
  8. References: <3100187d.5776685@ixnews7.ix.netcom.com>
  9. NNTP-Posting-Host: liv-26.rust.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. n4jvp@ix.netcom.com (n4jvp) wrote:
  13.  
  14. >    I have a question concerning ctors. Can a ctor call a member
  15. >method? 
  16.  
  17. Yes, it is perfectly valid for a constructor to call a method in the
  18. current class or a base class.
  19.  
  20. The following, however, is a common mistake made by beginners:
  21.  
  22.   class A
  23.   {
  24.   public:
  25.         A();
  26.         virtual void someFunc();
  27.   };
  28.  
  29.   class B : public A
  30.   {
  31.    public:
  32.         B();
  33.         virtual void someFunc();
  34.   };
  35.  
  36.   A::A()
  37.   {
  38.      someFunc();
  39.   }
  40.  
  41.   int main()
  42.   {
  43.       B *anObject = new B();
  44.       ...
  45.   }
  46.  
  47. The constructor for B (code not included above) will call the
  48. constructor for A, which in turn calls the method someFunc.  Many
  49. beginners think that since an instance of class B is being
  50. constructed, the someFunc() method called from the constructor of A
  51. will be B::someFunc().  This is _incorrect_.  A::someFunc() will be
  52. called, because the class B has not yet been completely constructed.
  53. Watch out for this trap.
  54.  
  55. Earl
  56.  
  57.    
  58.  
  59.